package com.itheima.service.impl;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.itheima.domain.SmsCode;
import com.itheima.service.SmsCodeService;
import com.itheima.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * ClassName: SmsCodeServiceImpl
 * Description:
 * Date: 2023/03/11 AM 12:32:36
 * <p>
 * project: springboot
 * package: com.itheima.service.impl
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
public class SmsCodeServiceImpl implements SmsCodeService {

    @Autowired
    private CodeUtils codeUtils;

    // remote
    // @CreateCache(area = "sms", name = "jetCache", expire = 10, timeUnit = TimeUnit.SECONDS)

    @CreateCache(name = "jetCache_", expire = 1000, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.LOCAL)
    private Cache<String, String> jetCache;

    /**
     * 发送验证码到手机
     *
     * @param tele {@link String}
     * @return {@link String}
     */
    @Override
    public String sendCodeToSms(String tele) {
        String code = codeUtils.generator(tele);
        jetCache.put(tele, code);
        return code;

    }

    /**
     * 检查验证码
     *
     * @param smsCode {@link String}
     * @return {@link Boolean}
     */
    @Override
    public boolean checkCode(SmsCode smsCode) {
        String code = jetCache.get(smsCode.getTele());
        return smsCode.getCode().equals(code);
    }

}
